** Preamble **
 
clear all
set more off

local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/tables"


* Note: local directory is the output folder 
cd "`path_output'"


					** Loading the input file **

use `path_input'/dataset_final_may20, clear

estimates clear

xtset codigo_ibge turno

local treatments            "Globo_Only Globo_Others SBT_Band"
local elec_outcomes_round_1 "sh_Tesquerda_1o Lula_1o sh_Tdireita_1o Collor_1o sh_non_valid_1o turnout_1o"
local elec_outcomes_round_2 "sh_Tesquerda_2o Lula_2o sh_Tdireita_2o Collor_2o sh_non_valid_2o turnout_2o"
local baseline_controls     "pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural"

gen sh_Tesquerda_2o = Lula_2o
gen sh_Tdireita_2o  = Collor_2o

gen Globo_Others=(Globo==1 & SBT_Bandeirantes==1)

gen     Globo_Only=0
replace Globo_Only=1 if Globo==1 & SBT_Bandeirantes==0

replace pop1991=pop1991/1000


										** Table: Balance check **
															
															
label var Globo_Only       "Globo only"
label var Globo_Others     "Globo and other broadcasters"
label var SBT_Bandeirantes "Other broadcasters"

label var sh_Tesquerda_1o  "Left-wing vote share" 
label var Lula_1o          "Lula's vote share" 
label var sh_Tdireita_1o   "Right-wing vote share"
label var Collor_1o        "Collor's vote share"
label var sh_non_valid_1o  "Non-valid Votes"
label var turnout_1o       "Turnout"

label var sh_Tesquerda_2o  "Left-wing vote share" 
label var Lula_2o          "Lula's vote share" 
label var sh_Tdireita_2o   "Right-wing vote share"
label var Collor_2o        "Collor's vote share"
label var sh_non_valid_2o  "Non-valid Votes"
label var turnout_2o       "Turnout"

label var pop1991          "Population [×1,000]"
label var years_sch1991    "Years of schooling"
label var renda1991        "Income per capita [in min. wage]"
label var sh_tv1991        "Share of households with TV"
label var sh_agua1991      "Share of households with piped water"
label var sh_elect1991     "Share of households with electricity"
label var sh_rural         "Share of pop. in rural areas"



foreach v of varlist * {

	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
	
}
														
															
															
/*
The table reports summary statistics of the national sample's main variables, including electoral outcomes and control variables. Panels A and B, respectively, report municipality level statistics about electoral outcomes for the first- and second-round vote shares. Panel C reports report municipality level statistics socioeconomic variables. Income per capita indicates the average municipal income in Brazilian minimum wage in 1991 (equivalent to USD 83.00 in 2019).
*/

local legend "The table reports summary statistics of the national sample's main variables, including electoral outcomes and control variables. Panels A and B, respectively, report municipality level statistics about electoral outcomes for the first- and second-round vote shares. Panel C reports report municipality level statistics socioeconomic variables. Income per capita indicates the average municipal income in Brazilian minimum wage in 1991 (equivalent to USD 83.00 in 2019)."

		  
			  
			  												** Panel A **

balancetable (mean if Globo==1) (mean if Globo==0) (diff Globo) `elec_outcomes_round_1' using Table_balance_national_panel_A.tex, replace 	/// 
			  wide(mean1 sd1 mean2 sd2 pval3) vce(cluster codigo_ibge) 																		/// 
			  pvalues varlabel nonumbers noobservations nopar          																		///
			  ctitles("Mean" "Std. dev." "Mean" "Std. dev." "p-value") 																		///
			  groups("Treatment" "Control", pattern(1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}))						
			 	  
			 
  			  												** Panel B **		

balancetable (mean if Globo==1) (mean if Globo==0) (diff Globo) `elec_outcomes_round_2' using Table_balance_national_panel_B.tex, replace 	/// 
			  wide(mean1 sd1 mean2 sd2 pval3) vce(cluster codigo_ibge) 																		/// 
			  pvalues varlabel nonumbers noobservations  nopar          																	///
			  ctitles("Mean" "Std. dev." "Mean" "Std. dev." "p-value") 																		///
			  groups("Treatment" "Control", pattern(1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}))									///

			  
  			  												** Panel C **

balancetable (mean if Globo==1) (mean if Globo==0) (diff Globo) `baseline_controls' using Table_balance_national_panel_C.tex, replace 	    /// 
			  wide(mean1 sd1 mean2 sd2 pval3) vce(cluster codigo_ibge) 																		/// 
			  pvalues varlabel nonumbers nopar          																					///
			  ctitles("Mean" "Std. dev." "Mean" "Std. dev." "p-value") 																		///
			  groups("Treatment" "Control", pattern(1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}))									///
	   		  prefoot("Number of Municipalities & 3922 & & 130 & & \\")
			  
			  
			  
include "https://raw.githubusercontent.com/steveofconnell/PanelCombine/master/PanelCombine.do"

panelcombine, use(Table_balance_national_panel_A.tex Table_balance_national_panel_B.tex Table_balance_national_panel_C.tex)   ///
paneltitles("Electoral outcomes, 1st Round" "Electoral outcomes, 2nd round"  "Socioeconomic controls") columncount(6)         /// 
save(Table_Descriptive.tex) cleanup


